package com.ruoyi.vehicles.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.AliyunOSSUtil;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.domain.SysVechicleDto;
import com.ruoyi.system.api.domain.SysVehicles;
import com.ruoyi.system.api.domain.UpdateUseUnitDTO;
import com.ruoyi.vehicles.param.AddVehiclesParam;
import com.ruoyi.vehicles.param.UpdateVehiclesParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.vehicles.mapper.AVehiclesMapper;
import com.ruoyi.vehicles.domain.AVehicles;
import com.ruoyi.vehicles.service.IAVehiclesService;

/**
 * 车辆Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@Service
public class AVehiclesServiceImpl implements IAVehiclesService
{
    @Autowired
    private AVehiclesMapper aVehiclesMapper;

    /**
     * 查询车辆
     *
     * @param id 车辆主键
     * @return 车辆
     */
    @Override
    public AVehicles selectAVehiclesById(Long id)
    {
        return aVehiclesMapper.selectAVehiclesById(id);
    }

    /**
     * 查询车辆列表
     *
     * @param aVehicles 车辆
     * @return 车辆
     */
    @Override
    public List<AVehicles> selectAVehiclesList(AVehicles aVehicles)
    {
        return aVehiclesMapper.selectAVehiclesList(aVehicles);
    }

    @Override
    public List<AVehicles> selectAvailableAVehiclesList(AVehicles aVehicles, Long deptId)
    {
        return aVehiclesMapper.selectAvailableAVehiclesList(aVehicles,deptId);
    }
    @Override
    public List<AVehicles> selectDispatchableAVehiclesList(AVehicles aVehicles)
    {
        return aVehiclesMapper.selectDispatchableAVehiclesList(aVehicles);
    }

    /**
     * 新增车辆
     *
     * @param param 车辆信息
     * @param sysUser 当前登录用户信息
     * @return 结果
     */
    @Override
    public int insertAVehicles(AddVehiclesParam param, SysUser sysUser)
    {
        // 上传文件
        String uploadFile = AliyunOSSUtil.uploadFile(param.getFile());

        // 判断车牌号是否重复
        AVehicles aVehicles =aVehiclesMapper.selectAVehiclesLicensePlate(param.getLicensePlate());
        if (aVehicles!= null&&aVehicles.getStatus()!=5){
            throw new RuntimeException("车牌号已存在,请重新输入");
        }

        //解析参数并将tocken中的部门信息放入车辆对象中去
        aVehicles= BeanUtil.toBean(param,AVehicles.class);
        aVehicles.setOrganizationId(sysUser.getDept().getDeptId());
        aVehicles.setOrganizationName(sysUser.getDept().getDeptName());

        //设置默认状态为0空闲,将图片地址
        aVehicles.setStatus(0L);
        aVehicles.setImage(uploadFile);

        return aVehiclesMapper.insertAVehicles(aVehicles);
    }

    /**
     * 修改车辆
     *
     * @param param 修改车辆
     * @return 结果
     */
    @Override
    public int updateAVehicles(UpdateVehiclesParam param)
    {
        // 上传文件
        String uploadFile = AliyunOSSUtil.uploadFile(param.getFile());

        // 判断车辆是否存在
        if(param.getId()==null||param.getId()==0){
            throw new RuntimeException("车辆id不能为空");
        }
        AVehicles aVehicles=aVehiclesMapper.selectAVehiclesById(param.getId());
        if(aVehicles==null){
            throw new RuntimeException("车辆不存在");
        }

        //将修改的数据写入到数据库中
        aVehicles.setImage(uploadFile);
        aVehicles.setDriver(param.getDriver());

        int i = aVehiclesMapper.updateAVehicles(aVehicles);
        if(i==0){
            throw new RuntimeException("修改车辆信息失败");
        }
        return i;
    }

    /**
     * 批量删除车辆
     *
     * @param ids 需要删除的车辆主键
     * @return 结果
     */
    @Override
    public int deleteAVehiclesByIds(Long[] ids)
    {
        List<AVehicles> aVehiclesList = new ArrayList<>();
        for (Long id : ids) {
            AVehicles aVehicles = aVehiclesMapper.selectAVehiclesById(id);
            //判断车辆状态是否为空闲
            if (aVehicles.getStatus()!=0){
                String statusName=aVehicles.getStatus()==1?"使用中":aVehicles.getStatus()==2?"维修中":aVehicles.getStatus()==3?"保养中":aVehicles.getStatus()==4?"报废":"已删除";
                throw new RuntimeException(aVehicles.getLicensePlate()+"该车牌车辆状态为"+statusName+"不能删除");
            }
            aVehicles.setStatus(5L);
            aVehiclesList.add(aVehicles);
        }
        //批量修改车辆状态
        int sum =aVehiclesMapper.updateAVehiclesStatus(aVehiclesList);
        if(sum!=ids.length){
            throw new RuntimeException("有车辆删除失败");
        }
        return sum;
    }

    /**
     * 删除车辆信息
     *
     * @param id 车辆主键
     * @return 结果
     */
    @Override
    public int deleteAVehiclesById(Long id)
    {
        //判断车辆id是否为空
        if(id==null||id==0){
            throw new RuntimeException("车辆id不能为空");
        }
        //判断车辆状态是否为空闲
        AVehicles aVehicles = aVehiclesMapper.selectAVehiclesById(id);
        if (aVehicles.getStatus()!=0){
            String statusName=aVehicles.getStatus()==1?"使用中":aVehicles.getStatus()==2?"维修中":aVehicles.getStatus()==3?"保养中":aVehicles.getStatus()==4?"报废":"已删除";
            throw new RuntimeException(aVehicles.getLicensePlate()+"该车牌车辆状态为"+statusName+"不能删除");
        }
        aVehicles.setStatus(5L);
        int sum = aVehiclesMapper.updateAVehicles(aVehicles);
        if(sum==0){
            throw new RuntimeException("删除车辆失败");
        }
        return sum;
    }

    @Override
    public int updateVehiclesStatus(Set<SysVehicles> sysVehicles) {
        int num = 0;
        for (SysVehicles sysVehicle : sysVehicles) {
            AVehicles aVehicles = aVehiclesMapper.selectAVehiclesById(sysVehicle.getId());
            if (aVehicles==null){
                throw new RuntimeException("车辆不存在");
            }
            //判断车辆状态是否为空闲
            if(sysVehicle.getStatus()!=0){
                if (aVehicles.getStatus() != 0 && aVehicles.getStatus()!=1 && aVehicles.getStatus()!=2) {
                    throw new RuntimeException("该车辆状态正在使用中");
                }
            }
            if (aVehicles.getStatus()==5){
                throw new RuntimeException("该车辆已删除");
            }
            aVehicles.setStatus(sysVehicle.getStatus());
            num++;
            aVehiclesMapper.updateAVehicles(aVehicles);
        }

        if(num!=sysVehicles.size()){
            throw new RuntimeException("修改状态失败");
        }
        return num;
    }

    @Override
    public int updateVehiclesUseUnit(UpdateUseUnitDTO updateUseUnitDTO) {
        int num = 0;
        Set<SysVehicles> sysVehicles = updateUseUnitDTO.getSysVehicles();
        for (SysVehicles sysVehicle : sysVehicles) {
            AVehicles aVehicles = aVehiclesMapper.selectAVehiclesById(sysVehicle.getId());
            if (aVehicles==null){
                throw new RuntimeException("车辆不存在");
            }
            //判断车辆状态是否为空闲
            if(sysVehicle.getStatus()!=0){
                if (aVehicles.getStatus() != 0 && aVehicles.getStatus()!=1 && aVehicles.getStatus()!=2) {
                    throw new RuntimeException("该车辆状态正在使用中");
                }
            }
            if (aVehicles.getStatus()==5){
                throw new RuntimeException("该车辆已删除");
            }
            aVehicles.setStatus(sysVehicle.getStatus());
            aVehicles.setUsingDepartmentId(updateUseUnitDTO.getCallingOrganizationId());
            num++;
            aVehiclesMapper.updateAVehicles(aVehicles);

        }

        if(num!=sysVehicles.size()){
            throw new RuntimeException("修改状态失败");
        }
        return num;
    }

    @Override
    public int removeUseUnit(Set<SysVehicles> sysVehicles) {
        int num = 0;
        for (SysVehicles sysVehicle : sysVehicles) {
            AVehicles aVehicles = aVehiclesMapper.selectAVehiclesById(sysVehicle.getId());
            if (aVehicles==null){
                throw new RuntimeException("车辆不存在");
            }
            //判断车辆状态是否为空闲
            if (aVehicles.getStatus() != 0) {
                throw new RuntimeException("该车辆状态正在使用中");
            }
            if (aVehicles.getStatus()==5){
                throw new RuntimeException("该车辆已删除");
            }
            num++;
            aVehiclesMapper.updateAVehiclesUsingDepartment(aVehicles);
        }

        if(num!=sysVehicles.size()){
            throw new RuntimeException("修改状态失败");
        }
        return num;
    }

    @Override
    public List<AVehicles> selectAVehiclesListDept(AVehicles aVehicles, SysUser sysUser) {
        //解析令牌中的部门信息
        Long deptId = sysUser.getDept().getDeptId();
        List<AVehicles> list =aVehiclesMapper.selectAVehiclesListDept(aVehicles,deptId);
        return list;
    }

    @Override
    public Set<SysVechicleDto> selectVehicles(Set<SysVehicles> sysVehicles) {
        Set<SysVechicleDto> set = new HashSet<>();
        for (SysVehicles sysVehicle : sysVehicles) {
            AVehicles aVehicles = aVehiclesMapper.selectAVehiclesById(sysVehicle.getId());
            SysVechicleDto sysVechicleDto = new SysVechicleDto();
            if (aVehicles==null){
                throw new RuntimeException("车辆不存在");
            }
            BeanUtil.copyProperties(aVehicles,sysVechicleDto);
            set.add(sysVechicleDto);
        }
        return set;
    }
}
